import 'package:fig/events/spider_event.dart';
import 'package:fig/sqlite/table_media_category.dart';
import 'package:fig/utils/config.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class HomeNoDataWidget extends StatefulWidget {
  const HomeNoDataWidget();

  @override
  State<HomeNoDataWidget> createState() => _HomeNoDataWidget();
}

class _HomeNoDataWidget extends State<HomeNoDataWidget> {
  SnackBar _snackBarSpiderSleeping = SnackBar(content: Text('请在首页配置中开启爬虫'));
  SnackBar _snackBarSpiderWorking = SnackBar(content: Text('刷新数据中，请稍后'));

  Future<void> _refresh() async {
    if (Config.autoSpider()) {
      Config.eventBus.fire(SpiderWorkingEvent(true));
      Scaffold.of(context).hideCurrentSnackBar();
      Scaffold.of(context).showSnackBar(_snackBarSpiderWorking);

      Future.delayed(Duration(seconds: 3), () async {
        // 如果爬取到数据了，就刷新界面
        if ((await TableMediaCategory().getCategories()).length > 0) {
          Config.eventBus.fire(SpiderChangedEvent());
        }
      });
    } else {
      //toast
      Scaffold.of(context).hideCurrentSnackBar();
      Scaffold.of(context).showSnackBar(_snackBarSpiderSleeping);
    }
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
        body: RefreshIndicator(
      child: ListView.builder(
        itemCount: 1,
        itemBuilder: (BuildContext context, int index) {
          return new Column(
            children: [
              Center(
                child: Padding(
                  padding: EdgeInsets.only(top: 350, bottom: 350),
                  child: Text('               请在设置中开启爬虫，\n等待一段时间后下拉刷新即可看到效果'),
                ),
              ),
              // new Divider(height: 0.2) // 分隔线
            ],
          );
        },
      ),
      onRefresh: _refresh,
    ));
  }
}
